package com.mhxy.controller;

import com.mhxy.handler.TaskRunHandler;
import com.mhxy.handler.TaskProgressHandler;
import com.mhxy.util.HwndUtil;
import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@Slf4j
public class StartController {

    public static ExecutorService executorService = Executors.newFixedThreadPool(6);

    public static void run(String hwndName, List<String> selectTasks) {
        log.info("选择启动的HWND:{}, 任务: {}", hwndName, selectTasks);

        HwndUtil.HwndEntity hwndEntity = HwndUtil.hwndsMap.get(hwndName);
        if (hwndEntity == null) {
            log.info("选择的HWND不存在!");
            return;
        }

        TaskProgressHandler.addTasks(hwndName, selectTasks);

        if (!TaskProgressHandler.checkHwndUserThreadIsRun(hwndName)) {
            executorService.execute(new TaskRunHandler(hwndName, hwndEntity.getHwnd()));
            TaskProgressHandler.setRun(hwndName, true);
            log.info("开启线程[{}], 执行任务: {}", hwndName, selectTasks);
        }
    }

}
